<template>
  <el-dialog
    :title="props.proType === 'add' ? '新增' : '编辑'"
    width="500"
    v-model="showThis"
    :before-close="closeDialog"
    :append-to-body="true"
    @close="closeDialog()"
  >
    <el-form :model="form" label-width="auto" style="max-width: 600px">
      <el-form-item label="机房名称">
        <el-input v-model="form.roomName" />
      </el-form-item>
      <el-form-item label="所属项目">
        <el-select
          filterable
          allow-create
          :reserve-keyword="false"
          v-model="form.projectName"
          placeholder="选择或输入所属项目"
        >
          <el-option label="眉山" value="shanghai" />
        </el-select>
      </el-form-item>
      <el-form-item label="组态类型">
        <el-select v-model="form.type" placeholder="组态类型">
          <el-option label="数据机房" value="数据机房" />
          <el-option label="冷源机房" value="冷源机房" />
        </el-select>
      </el-form-item>
      <el-form-item label="备注">
        <el-input v-model="form.remark" type="textarea" />
      </el-form-item>
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="showThis = false">取消</el-button>
        <el-button type="primary" @click="onSubmit"> 提交 </el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script lang="ts" setup>
  import { PropType, ref, watch, reactive, onMounted } from 'vue';
  import { ProgramItem } from '@/views/demo/program-manage/program-types';
  import { addProject } from '@/api/project';
  const emits = defineEmits(['closeAddDialog']);

  const props = defineProps({
    proList: {
      type: Object as PropType<ProgramItem>,
      default: () => {}
    },
    proType: {
      type: String,
      default: ''
    },
    rowData: {
      type: Object,
      default: () => {}
    },
    showAddProgram: {
      type: Boolean,
      default: false
    }
  });
  const showThis = ref(false);

  // do not use same name with ref
  const form = reactive({
    projectName: '',
    roomName: '',
    type: '',
    remark: ''
  });

  const onSubmit = async () => {
    if (props.proType === 'edit') {
      form.id = props.rowData.id;
    }
    const reData: any = await addProject(form);
    console.log(reData);
    if (reData.data) {
      showThis.value = false;
    }
  };
  watch(
    () => props.showAddProgram,
    (val) => {
      console.log(props.showAddProgram, val);
      showThis.value = val;
    }
  );
  onMounted(() => {
    showThis.value = props.showAddProgram;
  });
  const closeDialog = () => {
    emits('closeAddDialog');
  };
</script>

<style scoped></style>
